Lab 06 - Docker
Docker
Installation
May not be required in the laboratory.
Remove older versions of Docker:
sudo apt remove docker docker-engine docker.io containerd runcUpdate and install required packages:
sudo apt update sudo apt install ca-certificates curl gnupg lsb-releaseAdd Docker’s official GPG key:
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.ascSet up the stable Docker repository:
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get updateInstall Docker Engine, CLI, and Containerd:
sudo apt update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginVerify that Docker Engine is installed correctly by running the
hello-worldimage:sudo docker run hello-world(Optional) To run Docker commands without
sudo, create thedockergroup and add your user to it:sudo groupadd docker sudo usermod -aG docker $USER newgrp dockerYou can test if the above step was successful by running:
docker run hello-world^ Notice that you don’t need
sudoin front of the command.
The above steps and further documentation are also available here.
Docker Commands
List Docker images
docker imagesList all Docker containers
docker ps -aStart a container
docker start <container_name>Run an
interactive bash shell inside a container
docker exec -it <container_name> bashCreate
a new container and run an interactive bash shell inside
it
docker run -it <image_name> bashCopy a file from the host to a container
docker cp path/to/file/on/host <container_name>:path/to/file/in/containerCopy a file from a container to the host
docker cp <container_name>:path/to/file/in/container path/to/file/on/hostList volumes
docker volume lsCreate a volume
docker volume create my_volumeRemove a volume
docker volume rm my_volume🛠🔥 Task 1 🛠🔥
Read Docker’s get started (10 parts in total).
🛠🔥 Task 2 🛠🔥
Containerize the application from the previous class. The application should use bind mounts to write to a file located in the host’s filesystem.
- Hint 1: You can use the Python image as a base for your application image.
- Hint 2: You can install required packages (in this case
click) globally withpipduring the image build process.
Run the application and check the logs. Try enabling autostart of your container with system startup.
🛠🔥 Task 3 🛠🔥
Instead of mounting a host directory, you can use a Docker volume. Perform the following steps:
- Create a volume named
my_volume. - Create and run a container with the volume attached using:
docker run -it --name my_container -v my_volume:/data ubuntu - Create and save a file inside the container (e.g.,
echo "Hello from Docker!" > /data/hello.txt). - Exit the container with the
exitcommand. - Remove the container with:
docker rm my_container.
After thinking, check in the console
The following command will automatically remove the container after running and reading the text file:
docker run --rm -v my_volume:/data ubuntu cat /data/hello.txt